<?php

namespace app\api\controller;
use Admin\Controller\QRcode;
use think\Controller;
use think\Request;
use app\common;
use think\Db;
use app\api\model\Areas;
use app\api\model\User;
use app\api\model\Message;

/*
* Notes: 基础模块
* Create Date: 2018/10/27
* Create Time: 11:36
* Create by :张福来
* Param :
*/
class Base extends Controller {

 public function __construct(Request $request = null){}
    /*
    * Notes: 测试短信接口
    * Create Date: 2018/10/4
    * Create Time: 上午9:45
    * Create by :张福来
    * param :
    */
    public function testSms(){
        $phone = input("post.phone");
        $modoules = input("post.model");
        $code = json_decode(input("post.code"),true);
        $result = sendSMS($phone,$modoules,$code);
        if($result->Code != "OK"){
            return apiResponse("201","短信发送失败",$result);
        }else{
            return apiResponse("200","短信发送成功",$result);
        }
    }
    /*
    * Notes: 区域三级联动
    * Create Date: 2018/10/4
    * Create Time: 下午2:42
    * Create by :张福来
    * $parent_id : 父级id
    */
    public function selectarea(){
        $areas = new Areas();
        $parent_id = input("post.parent_id");
        if(empty($parent_id)){
            return apiResponse("201","id 不能为空","");
        }
        $result = $areas->selectarea($parent_id);
        if(empty($result)){
            return apiResponse("201","查询失败",$result);
        }else{
            return apiResponse("200","查询成功",$result);
        }
    }
    /*
    * Notes: 区域三级联动大鹏特制版 （带分页， 带模糊搜索）
    * Create Date: 2018/10/4
    * Create Time: 下午2:42
    * Create by :张福来
    * $parent_id : 父级id
    */
    public function selectareaDU(){
        $areas = new Areas();
        $w["parent_id"] = input("post.parent_id");
        $pagenum= input("post.pagenum");
        if(!empty(input("post.name"))){
            $name = input("post.name");
            $AreasIDparent_id = "AreasID".input("post.parent_id").$pagenum.$name;
            $w["area_name"] = array('LIKE','%'.trim($name).'%');
        }else{
            $AreasIDparent_id = "AreasID".input("post.parent_id").$pagenum."-";
        }
        if(empty($pagenum)){
            return apiResponse("201","pagenum 不能为空","");
        }
        if(empty(input("post.parent_id"))){
            return apiResponse("201","id 不能为空","");
        }
        $result = $areas->selectareaDU($AreasIDparent_id,$w,$pagenum);
        if(empty($result)){
            return apiResponse("201","无更多数据",$result);
        }else{
            return apiResponse("200","查询成功",$result);
        }
    }

    /**
    * Notes: 发送4位短信验证码
    * Create Date: 2018/10/4
    * Create Time: 上午9:45
    * Create by :lijie
    * param :   phone   手机号
    * param :   type    短信类型 1:回收obd  2.更绑手机号
    */
    public function sendSms($phone,$type,$ptype){

        if(empty($phone)){
            return apiResponse("201","手机号不能为空",'');
        }
        $verify = mt_rand(1000,9999);

        $code['code'] = $verify;
        // 类型不同，调用不同的短信模板
        if($type == 1){
            $result = sendSMS($phone,"SMS_138079632",$code);
        }if($type == 2){
            $result = sendSMS($phone,"SMS_139230931",$code);
        }

        if($result->Code != "OK"){
            return false;
        }else{
            cache('phone'.$ptype.$phone,$verify,300);
            if(cache("phone".$ptype.$phone)){
                return true;
            }else{
                return false;
            }
        }
    }
    /*
    * Notes:
    * Create Date: 2018/10/5
    * Create Time: 下午3:20
    * Create by :张福来
    * param :   u_id         用户id
    * param :   addorless    1 加   2 减
    * param :   intergral    积分数量
    * param :   operate      操作信息
    * $result : 200 成功
    * $result : 201 失败
    * $result : 202 用户积分不足
    */
    public function walletoperate($u_id,$addorless,$intergral,$operate){
        $userModel = new User();
        $result = $userModel->walletoperate($u_id,$addorless,$intergral,$operate);
        $message = $this->message(1,$u_id,"积分提醒",$operate);
        if($message && $result){
            return $result;
        }else{
            return false;
        }

    }
    /*
    * Notes: 信息提醒
    * Create Date: 2018/10/8
    * Create Time: 上午10:29
    * Create by :张福来
    * $type : 提醒类型 1系统
    * $u_id : 用户id
    * $title : 提醒标题
    * $content : 提醒内容
    */
    public function message($type,$u_id,$title,$content){
        $message = new Message();
        $addarray["type"] = $type;
        $addarray["u_id"] = $u_id;
        $addarray["title"] = $title;
        $addarray["content"] = $content;
        $addarray["ctime"] = time();
        $addarray["utime"] = time();
        $addarray["status"] = 0;
        if(empty($addarray["type"])) return "信息类型不能为空";
        if(empty($addarray["u_id"])) return "用户id不能为空";
        if(empty($addarray["title"])) return "信息标题不能为空";
        if(empty($addarray['content'])) return "信息内容不能为空";
        $result = $message->addMessage($addarray);
        return $result;
    }
    /**
     * Notes: 生成二维码
     * Create Date: 2018/10/6
     * Create by :lijie
     * param :   $url                    二维码跳转的url
     * param :   $img_url                保存路径
     * param :   $errorCorrectionLevel   容错级别
     * param :   $matrixPointSize        生成图片大小
     */
    public function getPng($url='',$img_url='',$errorCorrectionLevel="L",$matrixPointSize=15){
        vendor('qrcode.Qrcode');
        $qrcode = new QRcode();

//        $url = "http://api.zhongxiangtianxia.cn/index.php/Function/index/p_id/4/mo_id/38/f_id/128";
//        $img_url = 'D:/phpStudy/WWW/carstation/public/code/'.time().'.png';
        $res = $qrcode->png($url,$img_url,$errorCorrectionLevel,$matrixPointSize,2);
        return $res;
    }

    /**
     * Notes: 将目录操作成压缩包
     * Create Date: 2018/10/8
     * Create by :lijie
     * param :   $path                   文件路径
     * param :   $zip                    zip对象
     */
    public function addFileToZip($path='',$zip=''){

        $handler    = opendir($path);       // 打开当前文件夹
        if($handler){
            while(($filename = readdir($handler)) !==false){
                if($filename!='.' && $filename!='..'){          // 文件夹文件名字为'.'和‘..’，不要对他们进行操作
                    if(is_dir($path.'/'.$filename)){            // 如果读取的某个对象是文件夹，则递归
                        $this->addFileToZip($path.'/'.$filename,$zip);
                    }else {
//                        echo $filename."-----";echo "<br>";
                        $zip->addFile($path.$filename,basename($path.$filename));     //将文件加入zip对象
                    }
                }
            }
            @closedir($path);
        }else {
            return apiResponse('201','文件夹打开失败','');
        }
    }
}
